//
//  ASTodayTransToViewController.m
//  TestPrj
//
//  Created by 朱铅峰 on 2019/12/19.
//  Copyright © 2019 Xing. All rights reserved.
//

#import "ASTodayTransToViewController.h"
#import <Masonry.h>
#import "ASTodayTransFromViewController.h"
#import "ASTodayTransFromListCell.h"

@interface ASTodayTransToViewController ()<UINavigationControllerDelegate,UIViewControllerAnimatedTransitioning>

@end

@implementation ASTodayTransToViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"timg"]];
    [self.view addSubview:self.imageView];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(self.view.mas_width).multipliedBy(1.2);
    }];
    
    self.label = [[UILabel alloc] init];
    self.label.font = [UIFont systemFontOfSize:15];
    self.label.numberOfLines = 0;
    self.label.text = @"打发了即日起了人去二楼就去了人家去了人家离去而离去恶劣日前据了解去了热情而哦 i 今天去二年企鹅温柔里看见企鹅老人家去二姐请问而且我饿日激情而企鹅日前陪人去诶人去骗人了吗燃气轮机万二千二起来围绕解决确认去二姐去了节日；企鹅家人；企鹅家人；全家人去去二楼丘吉尔了丘吉尔。。。";
    [self.view addSubview:self.label];
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.equalTo(self.imageView.mas_bottom).offset(15);
    }];
    
    self.closeControl = [[UIControl alloc] init];
    [self.closeControl addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.closeControl];
    [self.closeControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(44);
        make.top.mas_equalTo([UIApplication sharedApplication].statusBarFrame.size.height);
        make.right.mas_equalTo(-10);
    }];
    
    UIImageView *closeIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"close"]];
    closeIconView.alpha = 0.75;
    [self.closeControl addSubview:closeIconView];
    [closeIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(30);
        make.top.mas_equalTo(7);
        make.left.mas_equalTo(7);
    }];
    
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.font = [UIFont systemFontOfSize:18];
    self.nameLabel.numberOfLines = 0;
    self.nameLabel.text = @"名称标签哦哦哦哦哦";
    [self.view addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo([UIApplication sharedApplication].statusBarFrame.size.height+10);
        make.right.mas_equalTo(-20);
    }];
}

- (void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
    self.navigationController.delegate = self;
}

- (void)viewDidDisappear:(BOOL)animated {
    
    [super viewDidDisappear:animated];
    
    self.navigationController.delegate = nil;
}

/// 退出
- (void)closeAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UINavigationControllerDelegate

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
    
    return self;
}

#pragma mark - UIViewControllerAnimatedTransitioning

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    
    return 1.0;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    
    if ([transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey] != self) {
        return;
    }
    
    UIView *containerView = [transitionContext containerView];
    
    //获取跳转后的页面
    ASTodayTransFromViewController *toVC = (ASTodayTransFromViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    //根据selectIndexPath获取对应的cell, selectIndexPath可以在跳转的时候传进来
    ASTodayTransFromListCell *cell = (ASTodayTransFromListCell *)[toVC.tableView cellForRowAtIndexPath:_fromIndexPath];
    //获取cell的背景
    UIView *cellBgImageView = cell.iconView;
    cellBgImageView.hidden = YES;
    
    toVC.view.alpha = 0;
    //将B添加到转场视图容器里
    [containerView addSubview:toVC.view];
    
    //获取当前页面
    ASTodayTransToViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    //获取当前的背景图
    UIView *bgImageView = fromVC.imageView;
    bgImageView.hidden = YES;
    
    //创建背景图做动画用
    UIView *screenshotView = [bgImageView snapshotViewAfterScreenUpdates:NO];
    //圆角与cell保持一致
    screenshotView.layer.masksToBounds = YES;
    screenshotView.layer.cornerRadius = 15;
    screenshotView.frame = [containerView convertRect:bgImageView.frame fromView:bgImageView.superview];
    //创建标签
    UILabel *typeL = [self copyLabel:self.nameLabel];
    [screenshotView addSubview:typeL];
    
    [containerView addSubview:screenshotView];
    
    //动画
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:0.5f initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        [containerView layoutIfNeeded];
        
        //隐藏当前视图
        fromVC.view.alpha = 0.0f;
        
        CGRect frame = [containerView convertRect:cellBgImageView.frame fromView:cellBgImageView.superview];
        screenshotView.frame = frame;
        
        toVC.view.alpha = 1.0;
        
    } completion:^(BOOL finished) {
        
        //一样，动画结束后该隐藏的隐藏，该删除的删除
        cellBgImageView.hidden = NO;
        
        bgImageView.hidden = YES;
        
        [screenshotView removeFromSuperview];
        
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}

- (UILabel *)copyLabel:(UILabel *)label {
    
    UILabel *newLabel = [[UILabel alloc] init];
    newLabel.text = label.text;
    newLabel.font = label.font;
    newLabel.alpha = label.alpha;
    newLabel.frame =  label.frame;
    newLabel.textColor = label.textColor;
    newLabel.textAlignment = label.textAlignment;
    
    return newLabel;
}

@end
